<?php
/**
 * @Copyright
 * Modified PLUS version by
 * Viktor Vogel {@link http://www.kubik-rubik.de}
 * Version: 1.5-6-5
 * Modified on 01-Sep-2010
 * Project Site {@link http://www.kubik-rubik.de/joomla-hilfe/plugin-easycalccheck-plus-joomla}
 *
 * Many thanks to bembelimen {@link http://wiki.joomla-nafu.de} for suggestions for improvement
 * Many thanks to Andy B. {@link http://www.caymanactive.com} for the english translation file
 * Many thanks to Yuriy K. {@link http://www.kharson.com} for the russian translation file
 *
 * @package    EasyCalcCheck
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath {@link http://www.easy-joomla.org}
 * @author     Created on 21-Jun-2009
 *
 * Inspired by
 * Calc check
 * @author ff-webdesign and his post on {@link http://www.joomlaportal.de/sicherheit-joomla-1-5-und-erweiterungen/166694-kontaktformular-endlich-einfach-spamfrei.html joomlaportal.de}
 * Hidden field
 * @author anyone and his post on {@link http://www.joomlaportal.de/634356-post7.html joomlaportal.de}
 *
 * Thanks to Barnum (Joomlaportal) - pdf creation problem in Version 1.5-4 (and earlier)
 *
 *	@license GNU/GPL
 *	This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Kein direkter Zugriff
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.plugin.plugin');

/**
 * EasyCalcCheck PLUS
 * System Plugin
 *
 * @package		EasyCalcCheckPlus
 * @subpackage	Plugin
 */
class plgSystemEasyCalcCheckPlus extends JPlugin
{
	/**
	* Constructor
	*
	* @access      protected
	* @param       object  $subject The object to observe
	* @param       array   $config  An array that holds the plugin configuration
	*/
	function __construct( &$subject, $config )
	{
		parent::__construct( $subject, $config );

		// Sprache aus Administrator einlesen
		$this->loadLanguage('', JPATH_ADMINISTRATOR);

	}

	/**
	* Formularfelder suchen und Schutzmechanismen ansetzen
	*
	* @return void
	*/
	public function onAfterRender()
	{
		// Nur im Frontend
		$app = JFactory::getApplication();
		
		if ( $app->isAdmin() ) 
		{ 
			return; 
		}

		$option = JRequest::getWord('option');
		$view = JRequest::getWord('view');
		$page = JRequest::getCmd('page');

		switch ($option)
		{			
			case 'com_shop':	
				if($view != 'basket') {return;};
			break;
			case 'com_user':
				if ( !$this->params->get('user_reg') ) { return; }
				if ( $view != 'register' ) { return; };
			break;

			case 'com_contact':
				if ( !$this->params->get('contact') ) { return; }
				if ( $view != 'contact' ) { return; };
			break;

			// Virtuemart Registrierung - 1.5-3
			case 'com_virtuemart':
				if ( !$this->params->get('virtuemart') ) { return; }
				if ( ($page != 'shop.registration') AND ($page != 'checkout.index') ) { return; };
			break;

			default:
				return;
			break;
		}


		// Verstecktes Feld gewählt
		if ( $this->params->get('type_hidden') ) 
		{
			// Zufallsvariable für Anzeige des versteckten Feldes - 1.5-2
			$lsrandom = mt_rand(1,5);
			// Zeilenbreite für Verschleierung - 1.5-4
			$input_size = 30;
			// Bei Nutzung von Layout Overrides, könnte die Ausgabe variieren. Suchstring sollte ggf. angepasst werden!
			// Bei BEEZ-Template bitte Option BEEZ aktivieren. 1.5-4
			if ( $option == 'com_contact' ) 
			{
				if ( $lsrandom == 1 ) 
				{
					$searchString = '<label for="contact_name">';
				} 
				elseif ( $lsrandom == 2 ) 
				{
					$searchString = '<label id="contact_emailmsg" for="contact_email">';
				} 
				elseif ( $lsrandom == 3 ) 
				{
					$searchString = '<label for="contact_subject">';
				} 
				elseif ( $lsrandom == 4 ) 
				{
					if (!$this->params->get('beez')) {
						$searchString = '<label id="contact_textmsg" for="contact_text">';
					} elseif ($this->params->get('beez')) {
						$searchString = '<label id="contact_textmsg" for="contact_text" class="textarea">';
					}
				} 
				elseif ( $lsrandom == 5 ) 
				{
					if (!$this->params->get('beez')) 
					{
						$searchString = '<label for="contact_email_copy">';
					} 
					elseif ($this->params->get('beez')) 
					{
						$searchString = '<label for="contact_email_copy" class="copy">';
					}
				}
			} 
			elseif ( $option == 'com_user' ) 
			{
				// Bei Registrierung Zeilenbreite von 40 - 1.5-4
				$input_size = 40;
				if ( $lsrandom == 1 ) 
				{
					$searchString = '<label id="namemsg" for="name">';
				} 
				elseif ( $lsrandom == 2 ) 
				{
					$searchString = '<label id="usernamemsg" for="username">';
				} 
				elseif ( $lsrandom == 3 ) 
				{
					$searchString = '<label id="emailmsg" for="email">';
				} 
				elseif ( $lsrandom == 4 ) 
				{
					$searchString = '<label id="pwmsg" for="password">';
				} 
				elseif ( $lsrandom == 5 ) 
				{
					$searchString = '<label id="pw2msg" for="password2">';
				}
			} 
			elseif ( $option == 'com_virtuemart' ) // Virtuemart Registrierung - 1.5-3
			{ 
				if ( $lsrandom == 1 ) 
				{
					$searchString = '<label for="email_field">';
				} 
				elseif ( $lsrandom == 2 ) 
				{
					$searchString = '<label for="username_field">';
				} 
				elseif ( $lsrandom == 3 ) 
				{
					$searchString = '<label for="password_field">';
				} 
				elseif ( $lsrandom == 4 ) 
				{
					$searchString = '<label for="password2_field">';
				} 
				elseif ( $lsrandom == 5 ) 
				{	// Hier nicht label von agreed_field, da sonst aufgrund von JS das versteckte Feld nicht funktioniert
					$searchString = '<input type="checkbox" id="agreed_field" name="agreed" value="1" class="inputbox" />';
				}
			}
			elseif ($option == 'com_shop')
			{
				if ( $lsrandom == 1 ) 
				{
					$searchString = '<input type="password" name="pass">';
				} 
				elseif ( $lsrandom == 2 ) 
				{
					$searchString = '<input type="password" name="pass2">';
				} 
				elseif ( $lsrandom == 3 ) 
				{
					$searchString = '<input type="text" value="" name="email" size="30">';
				} 
				elseif ( $lsrandom == 4 ) 
				{
					$searchString = '<input type="text" value="" name="fio">';
				} 
				elseif ( $lsrandom == 5 ) 
				{	// Hier nicht label von agreed_field, da sonst aufgrund von JS das versteckte Feld nicht funktioniert
					$searchString = '<input type="password" name="pass">';
				}
			}			
			// Zufallsvariable generieren
			$_SESSION["hidden_field"] = $this->random();
			$_SESSION["hidden_field_label"] = $this->random();
			
			// Label-Tag, um Verschleierung zu vergrößern - 1.5-4
			if ( !$this->params->get('beez') ) 
			{
				$addString = '<label for="'.$_SESSION["hidden_field_label"].'"></label><input type="text" id="'.$_SESSION["hidden_field_label"].'" name="'.$_SESSION["hidden_field"].'" size="'.$input_size.'" class="inputbox '.$_SESSION["hidden_class"].'" />';
			}
			elseif ( $this->params->get('beez') ) 
			{
				$addString = '<label for="'.$_SESSION["hidden_field_label"].'" class="'.$_SESSION["hidden_class"].'"></label><input type="text" id="'.$_SESSION["hidden_field_label"].'" name="'.$_SESSION["hidden_field"].'" size="'.$input_size.'" class="inputbox '.$_SESSION["hidden_class"].'" />';
			}
			$body = JResponse::getBody();
			$body = str_replace($searchString, $addString.$searchString, $body);
			JResponse::setBody($body);
		}

		// Ersetzung der Joomla-Inputfelder 1.5-4 - optional 1.5-5
		if ( $this->params->get('encode') ) 
		{
			$body = JResponse::getBody();
			$session = JFactory::getSession();

			// Zufallsvariablen setzen
			$random = $this->random();
			$random2 = $this->random();
			$random3 = $this->random();
			$random4 = $this->random();
			$random5 = $this->random();

			if ( ($option == 'com_contact') ) 
			{
				$session->set('name', $random, 'easycalccheck');
				$session->set('email', $random2, 'easycalccheck');
				$session->set('subject', $random3, 'easycalccheck');
				$session->set('text', $random4, 'easycalccheck');
				$session->set('email_copy', $random5, 'easycalccheck');

				$name_old = array ( 'name="name"' , 'name="email"' , 'name="subject"' , 'name="text"' , 'name="email_copy"' );
				$name_new = array ( 'name="'.$random.'"' ,  'name="'.$random2.'"' ,  'name="'.$random3.'"' ,  'name="'.$random4.'"' ,  'name="'.$random5.'"' );

				if (!$this->params->get('beez')) 
				{
					if ( $this->params->get('encode') == 2 ) 
					{
						$id_old = array ( 'class="inputbox' ,  'contact_email_copy' , 'contact_email', 'contact_emailmsg' , 'contact_name' , 'contact_subject' , 'contact_textmsg' , 'contact_text' );
						$id_new = array ( 'class="inputbox '.$this->random().'' ,  $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() );
					} 
					else 
					{
						$id_old = array ( 'class="inputbox' , 'contact_email_copy' , 'contact_email', 'contact_emailmsg' , 'contact_name' , 'contact_subject' , 'contact_textmsg' , 'contact_text' , 'required validate-email' , 'required' );
						$id_new = array ( 'class="inputbox '.$this->random().'' , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() );
					}
				} 
				elseif ($this->params->get('beez')) 
				{
					if ( $this->params->get('encode') == 2 ) 
					{
						$id_old = array ( 'contact_email_copy' , 'for="contact_email"' , 'id="contact_email"', 'contact_emailmsg' , 'contact_name' , 'contact_subject' , 'contact_textmsg' , 'class="inputbox"' );
						$id_new = array ( $this->random() , 'for="'.$ce = $this->random().'"' , 'id="'.$ce.'"' , $this->random() , $this->random() , $this->random() , $this->random() , 'class="inputbox '.$this->random().'"' );
					} 
					else 
					{
						$id_old = array ( 'contact_email_copy' , 'for="contact_email"' , 'id="contact_email"', 'contact_emailmsg' , 'contact_name' , 'contact_subject' , 'contact_textmsg' , 'required validate-email' , 'required' , 'class="inputbox"' );
						$id_new = array ( $this->random() , 'for="'.$this->random().'"' , 'id="'.$this->random().'"' , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , 'class="inputbox '.$this->random().'"' );
					}
				}
				$body = str_replace($name_old, $name_new , $body);
				$body = str_replace($id_old, $id_new , $body);
			} 
			elseif ( $option == 'com_user' ) 
			{
				$session->set('name', $random, 'easycalccheck');
				$session->set('email', $random2, 'easycalccheck');
				$session->set('username', $random3, 'easycalccheck');
				$session->set('password', $random4, 'easycalccheck');
				$session->set('password2', $random5, 'easycalccheck');

				$name_old = array ( 'name="name"' , 'name="email"' , 'name="username"' , 'name="password"' , 'name="password2"' );
				$name_new = array ( 'name="'.$random.'"' ,  'name="'.$random2.'"' ,  'name="'.$random3.'"' ,  'name="'.$random4.'"' ,  'name="'.$random5.'"' );

				if ( $this->params->get('encode') == 2 ) 
				{
					$id_old = array ( 'namemsg' , 'usernamemsg' , 'emailmsg' , 'pwmsg' , 'pw2msg' , '"email"' , 'for="password"' , 'id="password"' , '"password2"' , '"name"' , '"username"' , 'class="inputbox required"' );
				$id_new = array ( $this->random() ,  $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , 'for="'.$pw_for = $this->random().'"' , 'id="'.$pw_for.'"' , $this->random() , $this->random() , $this->random() , 'class="inputbox '.$this->random().'"' );
				} 
				else 
				{
					$id_old = array ( 'namemsg' , 'usernamemsg' , 'emailmsg' , 'pwmsg' , 'pw2msg' , '"email"' , 'for="password"' , 'id="password"' , '"password2"' , '"name"' , '"username"' , 'required validate-username' , 'required validate-email' , 'required validate-password' , 'required validate-passverify' , 'class="inputbox required"' );
				$id_new = array ( $this->random() ,  $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , 'for="'.$pw_for = $this->random().'"' , 'id="'.$pw_for.'"' , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , 'class="inputbox '.$this->random().'"' );
				}
				$body = str_replace($name_old, $name_new , $body);
				$body = str_replace($id_old, $id_new , $body);
			} 
			elseif ( $this->params->get('encode_virtuemart') AND $option == 'com_virtuemart' ) 
			{
				$session->set('email', $random, 'easycalccheck');
				$session->set('username', $random2, 'easycalccheck');
				$session->set('password', $random3, 'easycalccheck');
				$session->set('password2', $random4, 'easycalccheck');
				$session->set('agreed', $random5, 'easycalccheck');

				$name_old = array ( 'name="email"' , 'id="username_field" name="username"' , 'name="password"' , 'name="password2"' , 'name="agreed"' );
				$name_new = array ( 'name="'.$random.'"' ,  'id="username_field" name="'.$random2.'"' ,  'name="'.$random3.'"' ,  'name="'.$random4.'"' ,  'name="'.$random5.'"' );

				$id_old = array ( 'email_field' , 'username_field' , 'password_field' , 'password2_field' , 'agreed_field' , 'class="inputbox"' );
				$id_new = array ( $this->random() , $this->random() , $this->random() , $this->random() , $this->random() , 'class="inputbox '.$this->random().'"' );

				$body = str_replace($name_old, $name_new , $body);
				$body = str_replace($id_old, $id_new , $body);
			}
			JResponse::setBody($body);
		}
	//	echo 'hi';
		// Rechenaufgabe aktiviert
		if ( $this->params->get('type_calc') ) 
		{

			// Virtuemart Registrierung - 1.5-3
			if ( $option == 'com_virtuemart' ) // Button je nach gewählter Sprache finden 1.5-4
			{
				global $VM_LANG;
				$searchString = '<input type="submit" value="'.$VM_LANG->_('BUTTON_SEND_REG').'" class="button" onclick="return( submitregistration());" />';
			} 
			elseif($option == 'com_shop'){
			
				$searchString = '<div style="display:none;" class="captcha"></div>';
			}
			else
			{
				$searchString = '<button class="button validate" type="submit">';
			}

			// Globale Variable - 1.5-2
			$_SESSION["spamcheck"] = $this->random();
			$_SESSION["rot13"] = mt_rand(0,1);

			// Operator bestimmen
			if ( $this->params->get('operator') == 2 ) 
			{
				$tcalc = mt_rand(1,2);
			} 
			elseif ( $this->params->get('operator') == 1 ) 
			{
				$tcalc = 2;
			} 
			else 
			{
				$tcalc = 1;
			}

			// Operanden bestimmen
			$max_value = $this->params->get('max_value', 20);

			if ( ($this->params->get('negativ') == 0) AND ($tcalc == 2) ) 
			{
				$spamCheckNr1 = mt_rand($max_value / 2, $max_value);
				$spamCheckNr2 = mt_rand(1, $max_value / 2);
				if ( $this->params->get('operand') == 3 ) 
				{
					$spamCheckNr3 = mt_rand(0, $spamCheckNr1 - $spamCheckNr2);
				}
			} 
			else 
			{
				$spamCheckNr1 = mt_rand(1, $max_value);
				$spamCheckNr2 = mt_rand(1, $max_value);
				if ( $this->params->get('operand') == 3 ) 
				{
					$spamCheckNr3 = mt_rand(0, $max_value);
				}
			}

			// Speicherung des Ergebnisses als Session-Variable - 1.5-4
			$addString = '';

			if ( $tcalc == 1 ) // Addition 
			{ 
				if ( $_SESSION["rot13"] == 1 ) // ROT13 Kodierung - 1.5-2
				{  
					if ( $this->params->get('operand') == 2 ) 
					{
						$_SESSION["spamcheckresult"]  = str_rot13(base64_encode($spamCheckNr1 + $spamCheckNr2));
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$_SESSION["spamcheckresult"] = str_rot13(base64_encode($spamCheckNr1 + $spamCheckNr2 + $spamCheckNr3));
					}
				} 
				else 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$_SESSION["spamcheckresult"] = base64_encode($spamCheckNr1 + $spamCheckNr2);
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$_SESSION["spamcheckresult"] = base64_encode($spamCheckNr1 + $spamCheckNr2 + $spamCheckNr3);
					}
				}
			} 
			elseif ( $tcalc == 2 ) // Subtraktion
			{  
				if ( $_SESSION["rot13"] == 1 ) 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$_SESSION["spamcheckresult"] = str_rot13(base64_encode($spamCheckNr1 - $spamCheckNr2));
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$_SESSION["spamcheckresult"] = str_rot13(base64_encode($spamCheckNr1 - $spamCheckNr2 - $spamCheckNr3));
					}
				} 
				else 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$_SESSION["spamcheckresult"] = base64_encode($spamCheckNr1 - $spamCheckNr2);
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$_SESSION["spamcheckresult"] = base64_encode($spamCheckNr1 - $spamCheckNr2 - $spamCheckNr3);
					}
				}
			}

			// Anzeige der Rechenaufgabe - DIV Layer hinzugefügt - 1.5-6
			if ( !$this->params->get('beez') ) 
			{
				$addString .= '<div><label for="'.$_SESSION["spamcheck"].'">';
			} 
			elseif ( $this->params->get('beez') ) 
			{
				$addString .= '<div class="'.$_SESSION["label_calc_div"].'"><label for="'.$_SESSION["spamcheck"].'" class="'.$_SESSION["label_calc"].'">';
			}
			$addString .= '<b>'.JText::_('Spamcheck').': </b><b>';
			if( $tcalc == 1 ) 
			{
				if ( $this->params->get('converttostring') ) 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$addString .= $this->converttostring($spamCheckNr1).' '.JText::_('PLUS').' '.$this->converttostring($spamCheckNr2).' '.JText::_('equals').' ';
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$addString .= $this->converttostring($spamCheckNr1).' '.JText::_('PLUS').' '.$this->converttostring($spamCheckNr2).' '.JText::_('PLUS').' '.$this->converttostring($spamCheckNr3).' '.JText::_('equals').' ';
					}
				} 
				else 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$addString .= $spamCheckNr1.' '.JText::_('PLUS').' '.$spamCheckNr2.' '.JText::_('equals').' ';
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$addString .= $spamCheckNr1.' '.JText::_('PLUS').' '.$spamCheckNr2.' '.JText::_('PLUS').' '.$spamCheckNr3.' '.JText::_('equals').' ';
					}
				}
			} 
			elseif ( $tcalc == 2 ) 
			{
				if ( $this->params->get('converttostring') ) 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$addString .= $this->converttostring($spamCheckNr1).' '.JText::_('MINUS').' '.$this->converttostring($spamCheckNr2).' '.JText::_('equals').' ';
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$addString .= $this->converttostring($spamCheckNr1).' '.JText::_('MINUS').' '.$this->converttostring($spamCheckNr2).' '.JText::_('MINUS').' '.$this->converttostring($spamCheckNr3).' '.JText::_('equals').' ';
					}
				} 
				else 
				{
					if ( $this->params->get('operand') == 2 ) 
					{
						$addString .= $spamCheckNr1.' '.JText::_('MINUS').' '.$spamCheckNr2.' '.JText::_('equals').' ';
					} 
					elseif ( $this->params->get('operand') == 3 ) 
					{
						$addString .= $spamCheckNr1.' '.JText::_('MINUS').' '.$spamCheckNr2.' '.JText::_('MINUS').' '.$spamCheckNr3.' '.JText::_('equals').' ';
					}
				}
			}
			$addString .= '</label>';
			$addString .= '</b><input type="text" name="'.$_SESSION["spamcheck"].'" id="'.$_SESSION["spamcheck"].'" size="3" class="inputbox '.$this->random().'" value="" /> <b><span style="color:red">*</span></b>';
			$addString .= '</div>';

			// Warnhinweise einblenden
			if ( $this->params->get('warn_ref') ) 
			{
				$addString .= '<p><img src="includes/js/ThemeOffice/warning.png" alt="Warning" /> ';
				$addString .= '<strong>'.JText::_('Warning').'</strong><br /><small>'.JText::_('Warning desc').'</small>';
				if ( $this->params->get('converttostring')) 
				{
					$addString .= '<br /><small>'.JText::_('CONVERTWARNING').'</small><br />';
				}
				$addString .= '</p>';
			}
			$body = JResponse::getBody();
			$body = str_replace($searchString, $addString.$searchString, $body);
			JResponse::setBody($body);
		}

		// Zeitsperre - 1.5-2
		if ( $this->params->get('type_time') ) 
		{
			$_SESSION["time"] = time();
		}
		
		return;
	} // Ende Funktion onAfterRender

	/**
	*
	* Namen der Inputfelder zurücksetzen, damit die Komponenten sie verarbeiten können - 1.5-4
	* Setzung der CSS-Anweisung in der Funktion onAfterRoute, um Funktionalität bei SEO zu bewahren - 1.5-6
	*
	*/
	function onAfterRoute()
	{
		// CSS in Head-Bereich - nur im Frontend - 1.5-4 - nur bei benötigen Komponenten - 1.5-5
		$app = JFactory::getApplication();
		$option = JRequest::getWord('option');
		$task = JRequest::getWord('task');
		$format = JRequest::getWord('format');
		$page = JRequest::getCmd('page');
		$view = JRequest::getWord('view');

		// Nur im Frontend, VCARD Anforderung und PDF-Erzeugung - keine Ausführung von addCustomTag - 1.5-5
		if ( ($app->isAdmin()) OR ($task == 'vcard') OR ($format == 'pdf') ) 
		{ 
			return; 
		}

		if ( (($option == "com_contact") AND ($view == 'contact')) OR ($option == "com_user") OR (($option == "com_virtuemart") AND (($page == 'shop.registration') OR ($page == 'checkout.index'))) ) 
		{
			if ( ($this->params->get('type_hidden')) OR ($this->params->get('beez') AND $this->params->get('type_calc')) ) 
			{
				$head = array();
				if ( $this->params->get('type_hidden') ) 
				{
					$_SESSION["hidden_class"] = $this->random();
					$head[] = '<style type="text/css">.'.$_SESSION["hidden_class"].' {display: none;}</style>';
				}
				if ( $this->params->get('beez') AND $this->params->get('type_calc') ) 
				{
					$_SESSION["label_calc"] = $this->random();
					$head[] = '<style type="text/css">.'.$_SESSION["label_calc"].' {width:27em !important;}</style>';
					$_SESSION["label_calc_div"] = $this->random();
					$head[] = '<style type="text/css">.'.$_SESSION["label_calc_div"].' {overflow: hidden;}</style>';
				}
				$head = "\n" . implode("\n", $head) . "\n";
				$document =& JFactory::getDocument();
				$document->addCustomTag( $head );
			}
		}

		// Rücksetzung der Joomla-Inputfelder 1.5-4 - optional 1.5-5
		if ( $this->params->get('encode') ) 
		{
			$session = JFactory::getSession();
			if ( ($option == 'com_contact') AND ($view == 'contact') ) 
			{
				$name = $session->get('name', null, 'easycalccheck');
				$email = $session->get('email', null, 'easycalccheck');
				$subject = $session->get('subject', null, 'easycalccheck');
				$text = $session->get('text', null, 'easycalccheck');
				$email_copy = $session->get('email_copy', null, 'easycalccheck');

				$name_value = JRequest::getString($name);
				$email_value = JRequest::getString($email);
				$subject_value = JRequest::getString($subject);
				$text_value = JRequest::getString($text);
				$email_copy_value = JRequest::getString($email_copy);

				JRequest::setVar('name', $name_value);
				JRequest::setVar('email', $email_value);
				JRequest::setVar('subject', $subject_value);
				JRequest::setVar('text', $text_value);
				JRequest::setVar('email_copy', $email_copy_value);

				$session->clear('name','easycalccheck');
				$session->clear('email','easycalccheck');
				$session->clear('subject','easycalccheck');
				$session->clear('text','easycalccheck');
				$session->clear('email_copy','easycalccheck');
			} 
			elseif ( ($option == 'com_user') AND ($task == 'register_save') ) 
			{
				$name = $session->get('name', null, 'easycalccheck');
				$email = $session->get('email', null, 'easycalccheck');
				$username = $session->get('username', null, 'easycalccheck');
				$password = $session->get('password', null, 'easycalccheck');
				$password2 = $session->get('password2', null, 'easycalccheck');

				$name_value = JRequest::getString($name);
				$email_value = JRequest::getString($email);
				$username_value = JRequest::getString($username);
				$password_value = JRequest::getString($password);
				$password2_value = JRequest::getString($password2);

				JRequest::setVar('name', $name_value);
				JRequest::setVar('email', $email_value);
				JRequest::setVar('username', $username_value);
				JRequest::setVar('password', $password_value);
				JRequest::setVar('password2', $password2_value);

				$session->clear('name','easycalccheck');
				$session->clear('email','easycalccheck');
				$session->clear('username','easycalccheck');
				$session->clear('password','easycalccheck');
				$session->clear('password2','easycalccheck');
			} 
			elseif ( ($this->params->get('encode_virtuemart')) AND ($option == 'com_virtuemart') AND ($page == 'checkout.index') ) 
			{
				$email = $session->get('email', null, 'easycalccheck');
				$username = $session->get('username', null, 'easycalccheck');
				$password = $session->get('password', null, 'easycalccheck');
				$password2 = $session->get('password2', null, 'easycalccheck');
				$agreed = $session->get('agreed', null, 'easycalccheck');

				$email_value = JRequest::getString($email);
				$username_value = JRequest::getString($username);
				$password_value = JRequest::getString($password);
				$password2_value = JRequest::getString($password2);
				$agreed_value = JRequest::getString($agreed);

				JRequest::setVar('email', $email_value);
				JRequest::setVar('username', $username_value);
				JRequest::setVar('password', $password_value);
				JRequest::setVar('password2', $password2_value);
				JRequest::setVar('agreed', $agreed_value);

				$session->clear('email','easycalccheck');
				$session->clear('username','easycalccheck');
				$session->clear('password','easycalccheck');
				$session->clear('password2','easycalccheck');
				$session->clear('agreed','easycalccheck');
			}
		}
	} // Ende Funktion onAfterRoute

	/**
	 * Exception kann durch Komponente nicht verarbeitet werden
	 * Redirection durch PHP / JS durchgeführt und abgefangen
	 *
	 * @return void
	 */
	function onAfterInitialise()
	{
		if ( JRequest::getCmd('essp_err', '', 'get') == 'check_failed' ) 
		{
			JError::raiseWarning(100, JText::_('You have not resolved our spamcheck'));
		}
	} // Ende Funktion onAfterInitialise

	/**
	 * Checks the result of the calc check submittet by the contact form
	 *
	 * @param $contact
	 * @param $post
	 *
	 * @return mixed true on success / JExecption on failure
	 */
	public function onValidateContact( $contact, $post )
	{
		// Nur im Frontend
		$app = JFactory::getApplication();
		if ( $app->isAdmin() ) 
		{ 
			return; 
		}

		if ( $this->performChecks() ) 
		{
			return true;
		}

		// ID das Kontaktes auslesen - 1.5-3
		$id = JRequest::getCmd('id');
		$returnURI = JURI::current().'?option=com_contact&view=contact&id='.$id.'&essp_err=check_failed';

		// PHP Redirection
		header('Location: '.$returnURI);

		// JS Redirection
		?>
		<script type="text/javascript">window.location = '<?php echo $returnURI; ?>'</script>
		<?php

		// Weiße Seite (im Notfall)
		echo JText::_('You have not resolved our spamcheck');
		jexit();

	} // Ende Funktion onValidateContact

	/**
	 * Checks the result of the checks submittet by the registration form
	 *
	 * @param $user
	 * @param $isnew bool
	 *
	 * @return mixed true on succes - jexit() on failure as com_user does not hande plugin return values
	 */
	public function onBeforeStoreUser($user, $isnew)
	{
		// Nur im Frontend
		$app = JFactory::getApplication();
		if ( $app->isAdmin() ) 
		{ 
			return; 
		}

		// Nur bei Registrierungen
		if (!$isnew) 
		{
			return true;
		}

		if ( $this->performChecks() ) 
		{
			return true;
		}
		else
			return false;
		// Checks nicht bestanden
		$option = JRequest::getWord('option');

		// Virtuemart Registrierung - 1.5-3
		if ( $option == 'com_virtuemart' ) 
		{
			$returnURI = JURI::current().'?option=com_virtuemart&page=shop.registration&essp_err=check_failed';
		} 
		elseif($option == "com_shop"){
			$returnURI = JURI::current().'?option=com_shop&view=basket&essp_err=check_failed';
		}
		else
		{
			$returnURI = JURI::current().'?option=com_user&view=register&essp_err=check_failed';
		}

		// PHP Redirection
		header('Location: '.$returnURI);

		// JS Redirection
		?>
		<script type="text/javascript">window.location = '<?php echo $returnURI; ?>'</script>
		<?php

		// Weiße Seite (im Notfall)
		echo JText::_('You have not resolved our spamcheck');
		jexit();
	} // Ende Funktion onBeforeStoreUser

	/**
	 * checks the result of the form field checks from request
	 *
	 * @access private
	 *
	 * @return bool true on success
	 */
	private function performChecks()
	{
		// Rechenaufgabe prüfen
		if ( $this->params->get('type_calc') ) 
		{
			if ( $_SESSION["rot13"] == 1 ) 
			{
				$spamcheckresult = base64_decode(str_rot13($_SESSION["spamcheckresult"]));
			} 
			else 
			{
				$spamcheckresult = base64_decode($_SESSION["spamcheckresult"]);
			}
			$spamcheck = JRequest::getInt($_SESSION["spamcheck"], '', 'post');

			if ( !is_numeric($spamcheckresult) || $spamcheckresult != $spamcheck ) 
			{
				return false; // Aufgabe nicht gelöst
			}
			unset($_SESSION["spamcheckresult"]);
			unset($_SESSION["spamcheck"]);
			unset($_SESSION["rot13"]);
		}

		// Verstecktes Feld prüfen
		if ( $this->params->get('type_hidden') ) 
		{
			if ( JRequest::getVar($_SESSION["hidden_field"], '', 'post') ) 
			{
				return false; // Verstecktes Feld wurde ausgefüllt
			}
			unset($_SESSION["hidden_field"]);
		}

		// Zeitsperre prüfen
		if( $this->params->get('type_time') ) 
		{
			if ( time() - $this->params->get('type_time_sec') <= $_SESSION["time"] ) 
			{
				return false; // Eingabe zu schnell
			}
			unset($_SESSION["time"]);
		}
		return true;
	} // Ende Funktion performChecks

	/**
	 * returns a random string
	 *
	 * @access private
	 *
	 * @return random string
	 */
	private function random()
	{
		$pw = '';
		// 1. Zeichen muss ein Buchstabe sein
		$characters = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
		$array_a = explode(",",$characters);
		$z = mt_rand(0,25);
		$pw .= "".$array_a[$z]."";
		// restliche Zeichen beliebig
		$characters = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9";
		$array_b = explode(",",$characters);
		$pw_l = mt_rand(4,12);
		for ( $i=0; $i < $pw_l; $i++ ) 
		{
			srand((double)microtime()*1000000);
			$z = mt_rand(0,35);
			$pw .= "".$array_b[$z]."";
		}
		return $pw;
   } // Ende Funktion random

   	/**
	 * converts numbers into strings
	 *
	 * @access private
	 *
	 * @return string of a number
	 */
	private function converttostring($x)
	{
		// Wahrscheinlichkeit 2/3 für Konvertierung
		$random = mt_rand(1,3);

		if ( $random != 1) 
		{
			if ( $x > 20 ) 
			{
				return $x;
			} 
			else 
			{
				$lang = JFactory::getLanguage();

				if ( $lang->getName() == 'English (United Kingdom)' ) 
				{
					$names= array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty');
				} 
				elseif ( $lang->getName() == 'Deutsch (DE-CH-AT)' ) 
				{
					$names= array('null', 'eins', 'zwei', 'drei', 'vier', 'fünf', 'sechs', 'sieben', 'acht', 'neun', 'zehn', 'elf', 'zwölf', 'dreizehn', 'vierzehn', 'fünfzehn', 'sechzehn', 'siebzehn', 'achtzehn', 'neunzehn', 'zwanzig');
				}
				// Für andere Sprachen - Zahlen übersetzen und 4 Zeilen entkommentieren
				// else
				// {
				// $names= array('null', 'eins', 'zwei', 'drei', 'vier', 'fünf', 'sechs', 'sieben', 'acht', 'neun', 'zehn', 'elf', 'zwölf', 'dreizehn', 'vierzehn', 'fünfzehn', 'sechzehn', 'siebzehn', 'achtzehn', 'neunzehn', 'zwanzig');
				// }
				return $names[$x];
			}
		} 
		else 
		{
			return $x;
		}
	} // Ende Funktion converttostring

} // Ende Klasse
